<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-8-3
 * Time: 下午2:09
 */

namespace admin\modules\rbac\forms;


use common\models\db\Role;
use yii\base\Model;
use yii\db\Exception;

/**
 * Class MenuRole
 * @package admin\modules\rbac\forms
 *
 * @property array $menuRoles
 */
class MenuRole extends Model
{
    public $menu_id;
    public $roles;
    public $menu_roles;

    public function attributeLabels()
    {
        return [
            'menu_id' => "菜单",
            'roles' => "角色",
            'menu_roles' => "菜单角色",
        ];
    }

    public function rules()
    {
        return [
            ['menu_id', 'required'],
            ['roles', 'common\members\wodrow\validators\ArrayValidator'],
            ['menu_roles', 'common\members\wodrow\validators\ArrayValidator'],
        ];
    }

    /**
     * @return array
     */
    public function getMenuCheckedInAllRoles()
    {
        /**
         * @var Role[] $menus
         */
        $menus = Role::find()->all();
        $menu_roles = $this->getCheckedMenuRoles();
        $info = [];
        foreach ($menus as $k => $v){
            if (in_array($v->id, $menu_roles)){
                $info[] = [
                    'group' => null,
                    'name' => $v->name,
                    'value' => $v->id,
                    'checked' => true,
                    'parent' => null,
                ];
            }else{
                $info[] = [
                    'group' => null,
                    'name' => $v->name,
                    'value' => $v->id,
                    'checked' => false,
                    'parent' => null,
                ];
            }
        }
        return $info;
    }

    /**
     * @return array
     */
    public function getMenuRoles()
    {
        return [];
    }

    public function getCheckedMenuRoles()
    {
        $menu_roles = array_column(\common\models\db\RoleMenu::find()->select(['role_id'])->where(['menu_id' => $this->menu_id])->asArray()->all(), 'role_id');
        return $menu_roles?$menu_roles:[];
    }

    public function saveMenuRoles()
    {
        $old = $this->getCheckedMenuRoles();
        $new = $this->menu_roles;
        $add = array_diff($new, $old);
        $add_data = [];
        foreach ($add as $k => $v){
            $add_data[] = [
                'menu_id' => $this->menu_id,
                'role_id' => $v,
            ];
        }
        $remove = array_diff($old, $new);
        $remove_data = \common\models\db\RoleMenu::find()->where(['menu_id' => $this->menu_id])->andWhere(['in', 'role_id', $remove])->asArray()->all();
        $remove_data = array_column($remove_data, 'id');
        $trans = \Yii::$app->db->beginTransaction();
        try{
            \Yii::$app->db->createCommand()->batchInsert(\common\models\db\RoleMenu::tableName(), ['menu_id', 'role_id'], $add_data)->execute();
            if ($remove_data)\common\models\db\RoleMenu::deleteAll(['in', 'id', $remove_data]);
            \Yii::$app->session->addFlash("success", "保存成功");
            $trans->commit();
            return true;
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
    }
}